import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_author/config/ui_constant.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:star_author/widgets/button_widget.dart';

import '../../../utils/common_util/shared_preferences/sp_keys_constant.dart';
import 'model/notice_entity.dart';

class Notice extends StatefulWidget {
  final List<NoticeData>? data;
  final List<String>? localIds; //本地已经阅读了的公告id
  final String id;
  final Function? nextCallback;

  Notice({Key? key, this.data, this.localIds, this.id = "", this.nextCallback})
      : super(key: key);

  @override
  State<Notice> createState() => _NoticeState();
}

class _NoticeState extends State<Notice> {
  List<NoticeData> data = [];
  List<String> localIds = []; //本地已经阅读了的公告id
  String id = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    data = widget.data ?? [];
    localIds = widget.localIds ?? [];
    if (widget.id.isNotEmpty) {
      id = widget.id;
      int indexWhere =
          data.indexWhere((element) => element.id.toString() == id);
      if (indexWhere != -1) {
        if (indexWhere >= data.length - 1) {
          currentIndex = data.length - 1;
        } else {
          currentIndex = indexWhere + 1;
        }
      }
    }
  }

  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 280.w,
        child: Stack(
          children: [
            Container(
              constraints: BoxConstraints(
                minHeight: 310.w,
                maxHeight: 430.w,
              ),
              margin: EdgeInsets.only(top: 33.w),
              padding: EdgeInsets.only(
                top: 92.w,
                bottom: 61.w + 10.w,
                left: 20.w,
                right: 20.w,
              ),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(
                  Radius.circular(8.w),
                ),
              ),
              child: CupertinoScrollbar(
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    SelectableText(
                      '${data[currentIndex].body}',
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: UIConstant.color33,
                        height: 1.8,
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ],
                  padding: EdgeInsets.symmetric(horizontal: 20.w),
                ),
              ),
            ),
            Image.asset(
              'images/bg_notice_header_lt.png',
              width: 280.w,
            ),
            Positioned(
              bottom: 16.w,
              left: 0,
              right: 0,
              child: Center(
                child: Container(
                  child: ButtonWidget(
                    child: Text(
                      '知道了',
                      style: TextStyle(
                          fontSize: 16.sp,
                          color: Colors.white,
                          fontWeight: FontWeight.w500),
                    ),
                    type: ButtonType.bgOrange,
                    width: 120.w,
                    height: 40.w,
                    onPressed: () {
                      String? id = data[currentIndex].id.toString();
                      if (!localIds.contains(id)) {
                        localIds.add(id);
                        SpUtil.putStringList(SpKeysConstant.notice, localIds);
                      }
                      if (id == data.last.id.toString()) {
                        //最后一条消息
                        closeDialog();
                      } else {
                        Get.back();
                        currentIndex++;
                        Get.dialog(
                          Notice(
                            data: data,
                            localIds: localIds,
                            id: data[currentIndex].id.toString(),
                            nextCallback: widget.nextCallback,
                          ),
                          barrierDismissible: true, //是否可以通过空白点击
                          barrierColor: Colors.black54.withOpacity(0.3),
                        );
                      }
                    },
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void closeDialog() {
    Get.back();
    widget.nextCallback?.call();
  }

  buildContent() {
    if (data[currentIndex].body!.length > 200) {
      return Expanded(
        child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(top: 16.w, bottom: 24.w),
            child: SingleChildScrollView(
              child: CupertinoScrollbar(
                child: Text(
                  '${data[currentIndex].body}',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: UIConstant.color33,
                  ),
                ),
              ),
            )),
      );
    } else {
      return Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(top: 16.w, bottom: 24.w),
          child: SingleChildScrollView(
            child: CupertinoScrollbar(
              child: Text(
                '${data[currentIndex].body}',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: UIConstant.color33,
                ),
              ),
            ),
          ));
    }
  }
}
